﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace erse253_homework
{
    public partial class Scatterplot : BaseWindow
    {

        public Scatterplot(double[,] data1, double[,] data2)
        {
            InitializeComponent();

            double[] x;
            double[] y;
            GeneratesHistogramDistribution(data1, data2, out x, out y);

            GraphPane pane = zedGraphControl1.GraphPane;
            FormatScatterplot(x, y, pane);
        }

        public Scatterplot(double[,] data1, double[,] data2, double a, double b)
        {
            InitializeComponent();

            double[] x;
            double[] y;
            double[] x1;
            double[] y1;

            GeneratesHistogramDistribution(data1, data2, out x, out y);
            GenerateRegressionLine(a, b, x, out x1, out y1);

            //the zedGraphControl is a component developed by a third party programmer. (I'm using it here to speed up my development task).
            GraphPane pane = zedGraphControl1.GraphPane;

            FormatScatterplot(x, y, pane);
            FormatRegressionLine(x1, y1, pane, "y = " + Math.Round(a, 4).ToString() + "x + " + Math.Round(b, 4).ToString());

        }

        private static void FormatRegressionLine(double[] x1, double[] y1, GraphPane pane, string text)
        {
            LineItem linearRegression = pane.AddCurve("Regression Line: " + text, x1, y1, Color.DarkBlue, SymbolType.Circle);

            linearRegression.Line.IsVisible = true;
            linearRegression.Line.Width = 5.0F;
            linearRegression.Symbol.Size = 5.0F;
            linearRegression.Symbol.Fill = new Fill(Color.DarkBlue);
        }

        private void FormatScatterplot(double[] x, double[] y, GraphPane pane)
        {
            pane.Title.Text = "Scatterplot";
            pane.XAxis.Title.Text = "Value";
            pane.YAxis.Title.Text = "Value";

            LineItem myCurve = pane.AddCurve("Scatterplot", x, y, Color.Red, SymbolType.Circle);

            myCurve.Line.IsVisible = false;
            myCurve.Symbol.Size = 3.0F;
            myCurve.Symbol.Fill = new Fill(Color.Red);
            myCurve.Line.Fill.Type = FillType.GradientByZ;
            zedGraphControl1.AxisChange();
        }

        private static void GenerateRegressionLine(double a, double b, double[] x, out double[] x1, out double[] y1)
        {
            x1 = new double[2];
            y1 = new double[2];

            x1[0] = x.Min();
            x1[1] = x.Max();

            y1[0] = a * x1[0] + b;
            y1[1] = a * x1[1] + b;
        }

        private static void GeneratesHistogramDistribution(double[,] data1, double[,] data2, out double[] x, out double[] y)
        {

            x = new double[data1.Length];
            y = new double[data2.Length];

            //fill another array with the distribution of the data.
            for (int i = 0; i < data1.GetLength(0); i++)
                for (int j = 0; j < data1.GetLength(1); j++)
                {
                    x[i + j * data1.GetLength(0)] = data1[i, j];
                    y[i + j * data2.GetLength(0)] = data2[i, j];
                }
        }

    }
}
